<?php

ini_set('max_execution_time', 6000);
include_once dirname(dirname(dirname(__FILE__))) . '/include/common.php';
$core = new core();
function get_new_product() {
    $core = new core();
//    $query = "SELECT zcp.id,zcp.product_name,zcp.product_code,zcp.main_photo,zcp.thumb_main_photo,zcp.extra_photo,zcp.thumb_extra_photo,
//zcp.link_web,zcp.stamp_price,zcp.store_price,zcp.product_size,zcp.product_color
// FROM zen_crawl_product zcp,zen_product WHERE zcp.product_code = zen_product.product_code AND is_merged =0  ORDER BY id ASC LIMIT 1 ";
    $query = "SELECT * FROM zen_crawl_product WHERE is_merged =0  ORDER BY id ASC LIMIT 1 ";
    $list_nav = $core->db->get_row($query);
    if($list_nav ==null){
        return false;
    }
    return $list_nav;
}

$begin =time();
while ($product = get_new_product()) {
    var_dump("product id= ".$product->id);
    if (time() - $begin > 180) {
        die("het 3 phut running!!!");
    }
    var_dump("vao day");
    $core->db->query("UPDATE zen_crawl_product SET is_merged=1 WHERE id=$product->id");
    $old_product = $core->db->get_row("SELECT * FROM zen_product WHERE product_code ='".$product->product_code."'");

    $new_path_extra_photo = dirname(dirname(dirname(__FILE__))) . DS . "photo" . DS . "extra_photo" . DS;
    $new_path_extra_photo_thumb = dirname(dirname(dirname(__FILE__))) . DS . "photo" . DS . "extra_photo" . DS . "thumb_extra_photo" . DS;

    $new_path_main_photo = dirname(dirname(dirname(__FILE__))) . DS . "photo" . DS;
    $new_path_main_thumb_photo = dirname(dirname(dirname(__FILE__))) . DS . "photo" . DS . "thumb_photo" . DS ;
    //sản phẩm này chưa có, thêm mới

    if($old_product == null){
        var_dump("san pham cu khong thay");
        $is_error = false;
        $link_web = $product->link_web;
        $product_category = 0;
        if(stripos($link_web,"/woman/") !== false || stripos($link_web,"/trf/") !== false){
            $product_category = 2;
        }elseif(stripos($link_web,"/man/") !== false){
            $product_category = 1;
        }elseif(stripos($link_web,"/kids/") !== false || stripos($link_web,"/girl/") !== false || stripos($link_web,"/boy/") !== false){
            $product_category = 28;
        }else{
            $product_category = 0;
        }
        //chuyển extra photo
        $extra_photo_list_name = array();
        $array_extra_photo_list = explode(",",$product->extra_photo);
        $i=0;
        $main_photo = '';
        foreach ($array_extra_photo_list as $extra_photo_path) {

            $extra_photo_info = explode("/", $extra_photo_path);
            $extra_photo_name = $extra_photo_info[1];
            $extra_photo_list_name[] = $extra_photo_name;
            var_dump(dirname(dirname(dirname(__FILE__))) . DS . "tmp_photo_crawl" . DS . $extra_photo_path);
            if (file_exists(dirname(dirname(dirname(__FILE__))) . DS . "tmp_photo_crawl" . DS . $extra_photo_path)) {
                var_dump("file ton tai");
                rename(dirname(dirname(dirname(__FILE__))) . DS . "tmp_photo_crawl" . DS . $extra_photo_path, $new_path_extra_photo . $extra_photo_name);
                if($i==0){
                    $main_photo = $extra_photo_name;
                    copy($new_path_extra_photo . $extra_photo_name, $new_path_main_photo . $extra_photo_name);
                }
            }else{
                var_dump("file khong ton tai");
                $core->db->query("UPDATE zen_crawl_product SET is_merged=2,note='anh thumb main photo khong co:".dirname(dirname(dirname(__FILE__))) . DS . "tmp_photo_crawl" . DS . $extra_photo_path."' WHERE id=$product->id");

                $is_error = true;
                break;
            }
            $i++;

        }

        if($is_error){
            var_dump("khong ton tai anh extra photo.");
            break;
        }
        //chuyển thumb extra photo
        $array_thumb_extra_photo_list = explode(",",$product->thumb_extra_photo);
        $i=0;
        foreach ($array_thumb_extra_photo_list as $thumb_extra_photo_path) {

            $extra_photo_info = explode("/", $thumb_extra_photo_path);
            $thumb_extra_photo_name = $extra_photo_info[2];
            $thumb_extra_photo_name = substr($thumb_extra_photo_name,4);//với ảnh thumb extra photo thì bỏ 200_ ở đầu
            var_dump(dirname(dirname(dirname(__FILE__))) . DS . "tmp_photo_crawl" . DS . $thumb_extra_photo_path);
            if (file_exists(dirname(dirname(dirname(__FILE__))) . DS . "tmp_photo_crawl" . DS . $thumb_extra_photo_path)) {
                var_dump("file ton tai");
                rename(dirname(dirname(dirname(__FILE__))) . DS . "tmp_photo_crawl" . DS . $thumb_extra_photo_path, $new_path_extra_photo_thumb . $thumb_extra_photo_name);
                if($i==0){

                    copy($new_path_extra_photo_thumb . $thumb_extra_photo_name, $new_path_main_thumb_photo . "200_".$thumb_extra_photo_name);
                }
            }else{
                var_dump("file khong ton tai");
                $core->db->query("UPDATE zen_crawl_product SET is_merged=2,note='anh thumb main photo khong co:".dirname(dirname(dirname(__FILE__))) . DS . "tmp_photo_crawl" . DS . $thumb_extra_photo_path."' WHERE id=$product->id");
                var_dump(dirname(dirname(dirname(__FILE__))) . DS . "tmp_photo_crawl" . DS . $thumb_extra_photo_path);
                $is_error = true;
                break;
            }
            $i++;
        }

        if($is_error){
            var_dump("khong ton tai anh extra photo.");
            break;
        }


        $list_extra_photo = implode(",", $extra_photo_list_name);
        $manufacture ='ZARA';
        $product_name = $product->product_name;
        $product_code = $product->product_code;
        $product_color = $product->product_color;
        $product_size = $product->product_size;
        $description ='';
        $product_price = 0;
        $old_product_price=0;
        $store_price = $product->store_price ? $product->store_price : 0;
        $stamp_price = $product->stamp_price ? $product->stamp_price : 0 ;

        $core->db->query("INSERT INTO zen_product (`manufacture`,`product_name`,`product_code`,product_category,`main_photo`,
        `extra_photo`,`description`,`link_web`,`product_price`,`old_product_price`,
        `store_price`,`stamp_price`,`product_size`,`product_color`)
                        VALUES('$manufacture','$product_name','$product_code',$product_category,'$main_photo',
                            '$list_extra_photo','$description', '$link_web',$product_price,$old_product_price,
                        $store_price,$stamp_price,'$product_size','$product_color')");

        $product_id = $core->db->insert_id;
        if ($product_id > 0) {
            var_dump($product_code);
            $core->db->query("UPDATE zen_crawl_product SET is_merged=3 WHERE id=$product->id");
        } else {
            $core->db->query("UPDATE zen_crawl_product SET is_merged=2 WHERE id=$product->id");
        }
    }else{
        var_dump("thay sp cu");
        $is_error = false;
        //cập nhật lại size
        $old_size = $old_product->product_size;
        $old_size_array = explode(",",$old_size);
        $new_size = $product->product_size;
        $new_size_array = explode(",",$new_size);
        $size_merged_array = $old_size_array;
        foreach($new_size_array as $item_new_size){
            $check_duplicate = false;
            foreach($old_size_array as $item_old_size){
                if(trim($item_new_size)== trim($item_old_size)){
                    $check_duplicate =true;
                    break;
                }
            }

            if($check_duplicate==false){
                $size_merged_array[] = trim($item_new_size);
            }
        }

        $size_merged =implode(",",$size_merged_array);

        //cập nhật lại màu sản phẩm
        $old_color = $old_product->product_color;
        $old_color_array = explode(",",$old_color);
        $new_color = $product->product_color;
        $new_color_array = explode(",",$new_color);
        $color_merged_array = $old_color_array;
        $new_extra_photo_name_list = explode(",",$old_product->extra_photo);
        foreach($new_color_array as $item_new_color){
            $check_duplicate = false;
            foreach($old_color_array as $item_old_color){
                if(trim($item_new_color)== trim($item_old_color)){
                    $check_duplicate =true;
                    break;
                }
            }

            if($check_duplicate==false){
                $new_extra_photo_name_list[] = $core->convert_string_friendly($item_new_color . "_" . $product->product_code . "_" . $old_product->manufacture) . ".jpg";
                $color_merged_array[] = trim($item_new_color);
            }
        }

        $color_merged =implode(",",$color_merged_array);
        $new_extra_photo_name = implode(",",$new_extra_photo_name_list);
        //cập nhật ảnh extra theo các màu
        $array_extra_photo_list = explode(",",$product->extra_photo);

        foreach ($array_extra_photo_list as $extra_photo_path) {

            $extra_photo_info = explode("/", $extra_photo_path);
            $extra_photo_name = $extra_photo_info[1];
            $extra_photo_list_name[] = $extra_photo_name;
            var_dump(dirname(dirname(dirname(__FILE__))) . DS . "tmp_photo_crawl" . DS . $extra_photo_path);
            if (file_exists(dirname(dirname(dirname(__FILE__))) . DS . "tmp_photo_crawl" . DS . $extra_photo_path)) {
                var_dump("file ton tai");
                rename(dirname(dirname(dirname(__FILE__))) . DS . "tmp_photo_crawl" . DS . $extra_photo_path, $new_path_extra_photo . $extra_photo_name);

            }else{
                var_dump("file khong ton tai");
                $core->db->query("UPDATE zen_crawl_product SET is_merged=2,note='anh thumb main photo khong co:".dirname(dirname(dirname(__FILE__))) . DS . "tmp_photo_crawl" . DS . $extra_photo_path."' WHERE id=$product->id");

                $is_error = true;
                break;
            }


        }

        if($is_error){
            var_dump("khong ton tai anh extra photo.");
            break;
        }
        //chuyển thumb extra photo
        $array_thumb_extra_photo_list = explode(",",$product->thumb_extra_photo);

        foreach ($array_thumb_extra_photo_list as $thumb_extra_photo_path) {

            $extra_photo_info = explode("/", $thumb_extra_photo_path);
            $thumb_extra_photo_name = $extra_photo_info[2];
            $thumb_extra_photo_name = substr($thumb_extra_photo_name,4);//với ảnh thumb extra photo thì bỏ 200_ ở đầu
            var_dump(dirname(dirname(dirname(__FILE__))) . DS . "tmp_photo_crawl" . DS . $thumb_extra_photo_path);
            if (file_exists(dirname(dirname(dirname(__FILE__))) . DS . "tmp_photo_crawl" . DS . $thumb_extra_photo_path)) {
                var_dump("file ton tai");
                rename(dirname(dirname(dirname(__FILE__))) . DS . "tmp_photo_crawl" . DS . $thumb_extra_photo_path, $new_path_extra_photo_thumb . $thumb_extra_photo_name);

            }else{
                var_dump("file khong ton tai");
                $core->db->query("UPDATE zen_crawl_product SET is_merged=2,note='anh thumb main photo khong co:".dirname(dirname(dirname(__FILE__))) . DS . "tmp_photo_crawl" . DS . $thumb_extra_photo_path."' WHERE id=$product->id");
                var_dump(dirname(dirname(dirname(__FILE__))) . DS . "tmp_photo_crawl" . DS . $thumb_extra_photo_path);
                $is_error = true;
                break;
            }

        }

        if($is_error){
            var_dump("khong ton tai anh extra photo.");
            break;
        }

        //cập nhật category
        $link_web = $product->link_web;
        $product_category = 0;
        if(stripos($link_web,"/woman/") !== false || stripos($link_web,"/trf/") !== false){
            $product_category = 2;
        }elseif(stripos($link_web,"/man/") !== false){
            $product_category = 1;
        }elseif(stripos($link_web,"/kids/") !== false || stripos($link_web,"/girl/") !== false || stripos($link_web,"/boy/") !== false){
            $product_category = 28;
        }else{
            $product_category = 0;
        }

        $store_price = $product->store_price ? $product->store_price : 0;
        $stamp_price = $product->stamp_price ? $product->stamp_price : 0 ;
        //cập nhật product
        $core->db->query("UPDATE zen_product SET
            product_category=$product_category,extra_photo='$new_extra_photo_name',store_price=$store_price,
                     stamp_price=$stamp_price, product_size='$size_merged',product_color='$color_merged'
             WHERE product_id=$old_product->product_id");

        $core->db->query("UPDATE zen_crawl_product SET is_merged=3 WHERE id=$product->id");
        var_dump("thanh cong:".$old_product->product_code);

    }


}